<?php


namespace App\Controller\Cooperative;


use App\Annotation\OperationLog;
use App\Controller\AbstractController;
use App\Request\Cooperative\CooperativeUserIndexRequest;
use App\Request\Cooperative\CooperativeUserLoginRequest;
use App\Request\Cooperative\CooperativeUserSaveRequest;
use App\Request\Cooperative\OperateLogRequest;
use App\Request\Cooperative\SaveMeRequest;
use App\Service\Cooperative\CooperativeUserService;
use App\Service\Cooperative\OperatinLogService;
use App\Utils\Captcha;
use Hyperf\Di\Annotation\Inject;

class CooperativeUserController extends AbstractController
{

    /**
     * @Inject()
     * @var CooperativeUserService
     */
    protected $cooperativeUserService;

    /**
     * @Inject()
     * @var OperatinLogService
     */
    protected $operationLog;

    /**
     * 运营人员列表
     * @OperationLog(event="获取运营人员列表",platform="cooperative")
     */
    public function index(CooperativeUserIndexRequest $request)
    {
        $validated = $request->validated();
        $index = $this->cooperativeUserService->index($validated);
        return $this->response->success($index);
    }

    /**
     * 新增/编辑运营人员
     * @OperationLog(event="新增/编辑运营人员",platform="cooperative")
     */
    public function save($id, CooperativeUserSaveRequest $request)
    {
        $validated = $request->validated();
        $cooperativeUser = $this->cooperativeUserService->save($id, $validated);
        return $this->response->success($cooperativeUser);
    }

    /**
     * 删除运营人员
     * @OperationLog(event="删除运营人员",platform="cooperative")
     */
    public function delete(int $id)
    {
        $this->cooperativeUserService->delete($id);
        return $this->response->success();
    }

    /**
     * 登录
     */
    public function login(CooperativeUserLoginRequest $request)
    {
        $validated = $request->validated();
        $info = $this->cooperativeUserService->login($validated);
        return $this->response->success( $info);
    }

    /**
     * 获取当前登录用户信息
     * @OperationLog(event="获取当前登录用户信息",platform="cooperative")
     */
    public function me()
    {
        $cooperativeUser = $this->cooperativeUserService->me();
        return $this->response->success($cooperativeUser->toArray());
    }

    /**
     * @OperationLog(event="修改当前用户信息",platform="cooperative")
     * @param SaveMeRequest $request
     * @return mixed|\Psr\Http\Message\ResponseInterface
     */
    public function saveMe(SaveMeRequest $request)
    {
        $validated = $request->validated();
        $this->cooperativeUserService->saveMe($validated);
        return $this->response->success();
    }

    /**
     * 查看操作日志
     * @param OperateLogRequest $request
     * @return mixed|\Psr\Http\Message\ResponseInterface
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function operateLog(OperateLogRequest $request)
    {
        $validated = $request->validated();
        $data = $this->operationLog->queryLog($validated);
        return $this->response->success($data);
    }

    /**
     * 获取验证码
     * @return mixed|\Psr\Http\Message\ResponseInterface
     */
    public function captcha()
    {
        $captcha = di(Captcha::class);
        $data = $captcha->getCaptcha();
        return $this->response->success([
            'captcha_key' => $data['key'],
            'captcha_image' => $data['verify_code']
        ]);
    }

    /**
     * 是否接受客服
     * @OperationLog(event="操作是否接受客服",platform="cooperative")
     * @param int $id
     * @return mixed|\Psr\Http\Message\ResponseInterface
     */
    public function setIsCust(int $id)
    {
        $ret = $this->cooperativeUserService->setIsCust($id);
        return $this->response->success($ret);
    }


}